//
//  BCWalletExchangeInputDiplayView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/21.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCWalletExchangeInputDiplayView.h"

#import "DDCustomNumberInputView.h"

@interface BCWalletExchangeInputDiplayView ()

@property (nonatomic, strong) DDCustomNumberInputView *customInputView;

@end

@implementation BCWalletExchangeInputDiplayView

- (DDCustomNumberInputView *)customInputView {
    if (!_customInputView) {
        _customInputView = [[DDCustomNumberInputView alloc] initWithFrame:[DDCustomNumberInputView defaultBounds]];
    }
    return _customInputView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setInputViews];
        //
        self.inputTF.placeholderColor = [UIColor qmui_colorWithHexString:@"#D6D6D6"];
        //
        self.customInputView.textField = self.inputTF;
        //
        //
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.inputTF];
    }
    return self;
}
//
- (void)setInputViews {
    self.allButton = [QMUIButton new];
    [self.allButton setTitleColor:[UIColor darkMainColor] forState:UIControlStateNormal];
    self.allButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
    self.allButton.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 13.0);
    [self addSubview:self.allButton];
    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
    }];
    [self.allButton addTarget:self action:@selector(allButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    UIView *vLine = [UIView new];
    vLine.backgroundColor = [UIColor separatorColor];
    [self addSubview:vLine];
    [vLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.allButton.mas_left);
        make.centerY.mas_equalTo(self.allButton);
        make.width.mas_equalTo(1.0);
        make.height.mas_equalTo(13.0);
    }];
}

//
- (void)setFromCurrency:(UserWalletExchangeDataCurrencyModel *)fromCurrency {
    _fromCurrency = fromCurrency;
    //
    if (_fromCurrency) {
        self.currencyLabel.text = _fromCurrency.acronyms;
        [self.currencyLabel sizeToFit];
    }
    CGFloat offset = CGRectGetWidth(self.currencyLabel.frame);
    [self.inputTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.displayContentView).offset(-offset);
    }];
    
    //
    self.digits = _fromCurrency?_fromCurrency.scaler.length-1:0;
}

- (void)setAllTitle:(NSString *)allTitle {
    _allTitle = allTitle;
    //
    CGFloat offset = 0;
    if (allTitle.length) {
        [self.allButton setTitle:allTitle forState:UIControlStateNormal];
        [self.allButton sizeToFit];
        offset = CGRectGetWidth(self.allButton.frame);
    }
    //
    [self.displayContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(-offset);
    }];
}

#pragma mark - allButtonDidClickedAction
- (void)allButtonDidClickedAction {
    if (self.superview) {
        [self.superview endEditing:true];
    }
    //
    self.inputTF.text = self.fromCurrency.money;
}


#pragma mark - textFieldTextDidChangeNotification:
- (void)textFieldTextDidChangeNotification:(NSNotification *)noti {
    id object = noti.object;
    if (object) {
        if (object==self.inputTF) {
            NSString *text = self.inputTF.text;
            if (text.length==1&&[text isEqualToString:@"."]) {//第一个输入.时 补0
                self.inputTF.text = @"0.";
            }
            if (text.length>1) {
                //只能输入到小数点后两位
                NSRange pointRange = [text rangeOfString:@"."];
                if (pointRange.location!=NSNotFound) {//有小数点 就不能输入小数点了
                    //判断小数点是不是最后一位
                    NSString *lastChar = [text substringFromIndex:text.length-1];
                    if (pointRange.location==text.length-1&&[lastChar isEqualToString:@"."]) {//说明输入的最后一位是小数点
                        
                    }else if([lastChar isEqualToString:@"."]) {
                        //如果最后一位是小数点 并且已经有了小数点 不能输入
                        text = [text substringToIndex:text.length-1];
                        self.inputTF.text = text;
                    }
                    //小数点后几位
                    NSInteger pointNumber = self.digits;
                    if (text.length-(pointRange.location+1)>pointNumber) {
                        self.inputTF.text = [text substringToIndex:pointRange.location+pointNumber+1];
                    }
                }else {//没有小数点 位数不能超过100000
                    //                    if (text.length>6) {
                    //                        self.textField.text = [text substringToIndex:6];
                    //                    }
                }
            }
            //
            if (self.delegate && [self.delegate respondsToSelector:@selector(walletExchangeInputAmountTextChanged:)]) {
                [self.delegate walletExchangeInputAmountTextChanged:self.inputTF.text];
            }
            
        }
    }
}




- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.inputTF];
}

@end
